package com.new_wear.project.system.video.controller;

import java.util.List;

import com.new_wear.common.utils.file.FileUploadUtils;
import com.new_wear.framework.config.NewEraConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.new_wear.framework.aspectj.lang.annotation.Log;
import com.new_wear.framework.aspectj.lang.enums.BusinessType;
import com.new_wear.project.system.video.domain.CstVideo;
import com.new_wear.project.system.video.service.ICstVideoService;
import com.new_wear.framework.web.controller.BaseController;
import com.new_wear.framework.web.domain.AjaxResult;
import com.new_wear.common.utils.poi.ExcelUtil;
import com.new_wear.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 系统 - 视频Controller
 * 
 * @author new_wear
 * @date 2019-12-30
 */
@Controller
@RequestMapping("/system/video")
public class CstVideoController extends BaseController
{
    private String prefix = "system/video";

    @Autowired
    private ICstVideoService cstVideoService;

    @RequiresPermissions("system:video:view")
    @GetMapping()
    public String video()
    {
        return prefix + "/video";
    }

    /**
     * 查询系统 - 视频列表
     */
    @RequiresPermissions("system:video:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CstVideo cstVideo)
    {
        startPage();
        List<CstVideo> list = cstVideoService.selectCstVideoList(cstVideo);
        return getDataTable(list);
    }

    /**
     * 导出系统 - 视频列表
     */
    @RequiresPermissions("system:video:export")
    @Log(title = "系统 - 视频", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CstVideo cstVideo)
    {
        List<CstVideo> list = cstVideoService.selectCstVideoList(cstVideo);
        ExcelUtil<CstVideo> util = new ExcelUtil<CstVideo>(CstVideo.class);
        return util.exportExcel(list, "video");
    }

    /**
     * 新增系统 - 视频
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存系统 - 视频
     */
    @RequiresPermissions("system:video:add")
    @Log(title = "系统 - 视频", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file, CstVideo cstVideo)throws Exception {
        String filePath = NewEraConfig.getUploadPath();
        System.out.println(filePath);
        String fileName  = FileUploadUtils.upload(filePath, file);
        cstVideo.setAddress(fileName);
        return toAjax(cstVideoService.insertCstVideo(cstVideo));
    }

    /**
     * 修改系统 - 视频
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        CstVideo cstVideo = cstVideoService.selectCstVideoById(id);
        mmap.put("cstVideo", cstVideo);
        return prefix + "/edit";
    }

    /**
     * 修改保存系统 - 视频
     */
    @RequiresPermissions("system:video:edit")
    @Log(title = "系统 - 视频", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CstVideo cstVideo)
    {
        return toAjax(cstVideoService.updateCstVideo(cstVideo));
    }

    /**
     * 删除系统 - 视频
     */
    @RequiresPermissions("system:video:remove")
    @Log(title = "系统 - 视频", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cstVideoService.deleteCstVideoByIds(ids));
    }


    @RequestMapping("/seeVideoController/{id}")
    public String addadpos(@PathVariable("id")Integer id,ModelMap map) {
        CstVideo cstVideo = cstVideoService.selectCstVideoById(id);
        map.put("cstVideo",cstVideo);
        return prefix + "/seeVideoHtml";
    }
}
